package gofish.objects;

import gofish.json.JsonObject;
import gofish.servlets.GetGameState;
import gofish.servlets.JoinGroup;
import gofish.servlets.Refresh;
import gofish.servlets.StartGame;
import gofish.servlets.TakeTurn;

/**
 * Client is just meant to simulate what a real client will do
 * when using the program; in other words, it is just for testing
 * purposes. Once we get the server set up and Adam's code ready,
 * this class will no longer be needed.
 * Client has the same functions that Adam's code will have. They also
 * contact (what will become) the servlets in the same way.
 *
 * @author Matt Lauber
 */
public class Client {

	private Player self;
	String name;
	private Player opponent;
	private int gid;
	private int eid = -1;
	private Game game;
	
	public Client(String name, String op) {
		this.name = name;
		opponent = new Player(op);
		self = new Player(name);
	}
	
	public int getEid() {
		return this.eid;
	}
	
	public void startGame() {
		gid = new StartGame().doPost();
	}
	
	public void joinGroup() {
		new JoinGroup().doPost(gid, name);
	}
	
	public int takeTurn(String card) {
		
		return new TakeTurn().doPost(gid, self.getUserName(), opponent.getUserName(), card);
	}
	
	public int refresh() {
		
		int eid_diff = new Refresh().doPost(gid, eid);
		
		if (eid_diff > 0) {
			eid += eid_diff;
			getGameState();
		}
		else if (eid_diff < 0) {
			// the game has ended
			eid = -1;
			System.out.println(new JsonObject(game, self.getUserName()));
			System.out.println("Game Over");
		}
		// else do nothing
		
		return eid;
	}
	
	public void getGameState() { // correct name?
		game = new GetGameState().doPost(gid, self.getUserName());
		self = game.getPlayer(self.getUserName());
	}
}
